<template>
  <!-- Dashboard -->
  <div class="page-container">
    <el-form :inline="true" label-width="100px" label-suffix=":">
      <el-row>
        <el-col :span="6">
          <el-form-item label="用户姓名">
            <el-input v-model="formData.userName" style="width: 100%" placeholder="请输入用户姓名" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="问题描述">
            <el-input v-model="formData.problemDescription" style="width: 100%" placeholder="请输入问题描述" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="处理状态"> 
            <el-select v-model="formData.handlingStatus" placeholder="请选择处理状态">
              <el-option label="全部" value=""></el-option>
              <el-option label="已处理" :value="true"></el-option>
              <el-option label="未处理" :value="false"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 查询按钮 -->
        <el-col :span="6">
          <el-button type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
          <el-button plain icon="el-icon-refresh" @click="resetForm">重置</el-button>
          <el-button v-if="userType === 'NORMAL_USER'" type="primary" icon="el-icon-plus" @click="$refs.mDialogFeek.open()">新增问题反馈</el-button>
        </el-col>
      </el-row>
    </el-form>
    <div stylle="height: calc(100vh - 160px)">
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row
        style="height: 500px">
        <el-table-column align="center" label="用户姓名" prop="userName"></el-table-column>
        <el-table-column align="center" label="问题描述" width="500" show-overflow-tooltip prop="problemDescription"></el-table-column>
        <el-table-column align="center" label="反馈时间" prop="feedbackTime"></el-table-column>
        <el-table-column align="center" label="处理状态" prop="handlingStatus">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.handlingStatus == true">已处理</el-tag>
            <el-tag type="warning" v-else-if="scope.row.handlingStatus == false">未处理</el-tag>
          </template>
        </el-table-column>
        <!-- v-if="userType === 'MANAGER_USER'" -->
        <el-table-column align="center" label="操作" v-if="userType === 'MANAGER_USER'">
          <template slot-scope="scope">
            <span class="el-icon-e" v-if="scope.row.handlingStatus == false" @click="bindDeel(scope.row)">处理</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 20px; text-align: center">
      <!-- 页码改变和页数改变事件 -->
      <el-pagination v-show="list" background layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total"
        :current-page="pageInfo.currentPage" :page-sizes="[10, 20, 30, 40, 50]" style="float: right"
        @current-change="currentPageChange" @size-change="sizeChange" />
    </div>


    <m-dialog-feek ref="mDialogFeek" @refreshIntersectionData="fetchData"></m-dialog-feek>

  </div>
</template>

<script>
import { mapGetters } from "vuex";
import mDialogFeek from "./components/feekBackPopup.vue";
import { getTmUserFeedbackPage, updateTmUserFeedback } from '@/api/tm-road'

export default {
  name: "FeedbackAndAlarm",
  computed: {
    ...mapGetters(["name", 'userType']),
  },
  components: {
    mDialogFeek
  },
  data() {
    return {
      formData: {
        userName: "",
        problemDescription: "",
        handlingStatus: null,
      },
      dateRange: [],
      listLoading: false,
      list: [
      ], // 列表数据
      pageInfo: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取列表数据
    async fetchData() {
      this.listLoading = true;
      const params = {
        userName: this.formData.userName,
        problemDescription: this.formData.problemDescription,
        handlingStatus: this.formData.handlingStatus,
        pageNum: this.pageInfo.currentPage,
        pageSize: this.pageInfo.pageSize,
      }
      const { code = undefined, items = {} } = await getTmUserFeedbackPage({ ...params })
      if (code === 200) {
        this.list = items.records;
        this.pageInfo.total = items.total
        setTimeout(() => {
          this.listLoading = false
        }, 200)
      } else {
        this.$message.error('获取数据失败')
        this.listLoading = false
      }
    },
    // 重置表单
    resetForm() {
      this.formData = {
        userName: "",
        problemDescription: "",
        handlingStatus: null
      };
      this.fetchData()
    },
    // 页码改变事件
    currentPageChange(currentPage) {
      this.pageInfo.currentPage = currentPage;
      this.fetchData()
    },
    // 页数改变事件
    sizeChange(pageSize) {
      this.pageInfo.pageSize = pageSize;
      this.fetchData()
    },
    // 处理问题
    bindDeel(row) {
      // 确认弹窗
      this.$confirm('确认处理该问题？')
       .then(async () => {
          // 处理问题
          row.handlingStatus = true
          const { code = undefined } = await updateTmUserFeedback({ ...row })
          if (code === 200) {
            this.$message.success('处理成功')
            this.fetchData()
          } else {
            this.$message.error('处理失败')
          }
        })
       .catch(() => {
          // 取消处理
       })
    }
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

p {
  margin: 0;
  padding: 0;
}

#clock {
  font-family: 'Share Tech Mono', monospace;
  color: #ffffff;
  text-align: center;
  color: #3F9EFF;
  text-shadow: 0 0 3px yellow;

  .time {
    letter-spacing: 0.05em;
    font-size: 20px;
    padding: 5px 0;
  }

  .date {
    letter-spacing: 0.1em;
    font-size: 18px;
  }

  .text {
    letter-spacing: 0.1em;
    font-size: 12px;
    padding: 20px 0 0;
  }
}
</style>